using System;
using System.Windows.Forms;
using DataModel.Reports;
using DevExpress.XtraEditors;
using DataModel;
using DevExpress.XtraEditors.Controls;

namespace RitaReports.Expenses
{
    public partial class ExpensesFilter : XtraUserControl, IReportFilter
    {
        private bool inited;

        public bool Inited
        {
            get { return inited; }
        }

        public ExpensesFilter()
        {
            InitializeComponent();
        }


        #region IReportFilter Members

        public Control FilterControl
        {
            get { return this; }
        }

        public IReportFilterConditionds FilterConditionds
        {
            get
            {
                ExpensesFilterConditionds conditionds = new ExpensesFilterConditionds();

                if (cmbOrganization.Properties.Items.Count > 0 &&
                    cmbOrganization.SelectedIndex != 0)
                {
                    conditionds.OrgId = (int) cmbOrganization.Properties.Items[cmbOrganization.SelectedIndex].Value;
                }

                if (cmbSeniority.Properties.Items.Count > 0 &&
                    cmbSeniority.SelectedIndex != 0)
                {
                    conditionds.Seniority = cmbSeniority.Text;
                }

                if (!string.IsNullOrEmpty(txtDestinations.Text.Trim()))
                {
                    conditionds.Destination = txtDestinations.Text;
                }

                if (deDateFrom.EditValue != null || deDateTo.EditValue != null)
                {
                    DateTime? from = null;
                    DateTime? to = null;

                    if (deDateFrom.EditValue != null)
                    {
                        from = (DateTime) deDateFrom.EditValue;
                    }

                    if (deDateTo.EditValue != null)
                    {
                        to = (DateTime) deDateTo.EditValue;
                    }

                    conditionds.Date = new Range<DateTime?>(from, to);
                }
                
                return conditionds;
            }
        }

        #endregion


        public void Init(TravelsReportInitData initData)
        {
            cmbOrganization.Properties.Items.Clear();
            cmbOrganization.Properties.Items.Add(new ImageComboBoxItem("All", 0, 3));
            foreach (Organization org in initData.Organizations)
            {
                if (org.IsTravelAgency)
                {
                    continue;
                }
                cmbOrganization.Properties.Items.Add(new ImageComboBoxItem(org.Name, org.Id, org.IsTravelAgency ? 0 : 1));
            }
            cmbOrganization.SelectedIndex = 0;

            cmbSeniority.Properties.Items.Clear();
            cmbSeniority.Properties.Items.Add(new ImageComboBoxItem("All", 3));
            foreach (string senior in initData.Seniorities)
            {
                cmbSeniority.Properties.Items.Add(new ImageComboBoxItem(senior, 2));
            }
            cmbSeniority.SelectedIndex = 0;

            inited = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            cmbOrganization.SelectedIndex = 0;
            cmbSeniority.SelectedIndex = 0;
            txtDestinations.Text = "";
            deDateFrom.EditValue = null;
            deDateTo.EditValue = null;
        }
    }
}